package com.ys.controller.web.business;

import com.bo.vo.W;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.business.*;
import com.ys.enume.business.TpProductMaterialEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Listing库
 */
@Controller
@RequestMapping("/web/business/tpProductActive")
public class WebTpProductActiveController {
	private static final Logger logger = LoggerFactory.getLogger(WebTpProductActiveController.class);
	private static Long MENU_ID = 10184L;
	private static Long MENU_ID_OPERATIONS = 10186L;//运营维护
	private static Long MENU_ID_STORAGE = 10187L;//仓储维护
	private static Long MENU_ID_LOGISTICS = 10188L;//物流维护
	private static Long MENU_ID_PURCHASE = 10189L;//采购维护
	private static Long MENU_ID_MANAGE = 10190L;//管理维护
	private static Long MENU_ID_WD = 10194L;//关联网店商品

	@Autowired
	private CommonApiService<TpProductActive> commonApiService;

	@Autowired
	private CommonApiService<TpAmazonShopProduct> tpAmazonShopProductService;


	/*
	 * 修改、详情页
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id,String editType) {
		try {
			//额外参数

			Map<String,Object> entity = new TpProductActive().setId(id)
					.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = A.VgsBm")
					.addField("THRO.VGsMc AS vgsMc,THRO.VGsJc AS vgsJc")
					.addLeftJoin("TB_BaseinfoSppl K ON  K.id = A.CommodityPlId")
					.addLeftJoin("TB_BaseinfoDwmc G ON  G.id = A.NameUnitId")
					.addLeftJoin("TB_BaseinfoSppm W ON  W.id = A.NameId")
					.addLeftJoin("TB_BaseinfoSpyt H ON  H.id = A.UsageId")
					.addLeftJoin("TB_BaseinfoLmwh O ON  O.id = A.CommodityLmId")
					.addField("K.categoryCn AS commodityPl")
					.addField("G.NameUnitCn AS nameUnit")
					.addField("W.NameCn AS nameCn,W.NameEn AS nameEn ")
					.addField("H.UsageCn AS usageCn,H.UsageEn AS usageEn ")
					.addField("O.CategoryPath AS vspLm1")
					.queryFirstMap();

			List<TpProductMaterial> tpProductMaterialList = new TpProductMaterial().setType(TpProductMaterialEnum.Type.TP_ProductActive.value).where("A.ProductCode = '" + entity.get("code").toString() + "'").queryList();

			if (tpProductMaterialList.size() != 0){
				// 处理商品材质列表
				StringBuilder materialCnList = new StringBuilder();
				StringBuilder materialEnList = new StringBuilder();
				StringBuilder materialIdList = new StringBuilder();
				for (int i = 0; i < tpProductMaterialList.size(); i++) {
					materialCnList.append(","+tpProductMaterialList.get(i).getMaterialNameCn());
					materialEnList.append(","+tpProductMaterialList.get(i).getMaterialNameEn());
					materialIdList.append(","+tpProductMaterialList.get(i).getMaterialId());
				}
				entity.put("materialCn",materialCnList.substring(1,materialCnList.length()));
				entity.put("materialEn",materialEnList.substring(1,materialEnList.length()));
				entity.put("materialId",materialIdList.substring(1,materialIdList.length()));
			}
			//获取子件数据
			List<Map<String, Object>> itemList = new TpProductBasisComponent().setPcode(entity.get("code").toString()).queryMap();

			Long menuId = null;
			int onProcess = 0;//可修改
			int onProcessBySon = 1;//子件是否可修改
			if(editType.equals("detail")){//详情页面
				menuId = MENU_ID;
				onProcess = 1;//不可修改

			}else if(editType.equals("operations")){//运营维护
				menuId = MENU_ID_OPERATIONS;

			}else if(editType.equals("storage")){//仓储维护
				menuId = MENU_ID_STORAGE;

			}else if(editType.equals("logistics")){//物流维护
				menuId = MENU_ID_LOGISTICS;

			}else if(editType.equals("purchase")){//采购维护
				onProcessBySon = 0;//子件可修改
				menuId = MENU_ID_PURCHASE;

			}else if(editType.equals("manage")){//管理维护
				menuId = MENU_ID_MANAGE;

			}else {
				return W.error(model, "未知的类型");
			}

			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);// 子列表配置信息
			if(onProcessBySon == 1){//子表不可修改
				subGridConfig.setButtonArr(new ArrayList<>());//子表按钮
			}
			model.addAttribute("onProcess", onProcess);//母表是否可修改

			if(onProcess==1) {//母表是否可修改
				return commonApiService.toDetail(model,id, new TpProductActive(), menuId, entity, subGridConfig);
			}else {
				commonApiService.toEdit(model,id, new TpProductActive(), menuId, entity, subGridConfig);
				return "business/tpProductActive/toEdit";
			}


		}catch (Exception e) {
			logger.error("Listing库修改、详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}



	//关联平台编码
	@GetMapping("/bindShop")
	public String bindShop(Model model,String code){
		try {
			if (code == null){
				return com.ys.vo.W.error(model, "商品编码不能为空!");
			}

			TpProductActive tpProductActive = new TpProductActive().setCode(code).queryFirst();
			if (tpProductActive == null){
				return com.ys.vo.W.error(model, "Listing数据为空!");
			}

			//权限
			TpAmazonShopProduct cond = new TpAmazonShopProduct();
			cond.setCode(code);
			cond.addLeftJoin("TP_AmazonShop t2 on A.ShopId = t2.id");
			cond.addField("t2.shopName as shopName,t2.sellerId as sellerId,t2.sellerId as sellerId");
			cond.addLeftJoin("TP_AmazonSeller tsell on tsell.id = t2.amazonSellerId");//卖家管理
			cond.addField("tsell.regionName as marketplaceName");
			cond.addLeftJoin("TB_Site ts on ts.id = t2.SiteId");//站点
			cond.addField("ts.AmazonMarketplaceId AS marketplaceId");
			cond.addLeftJoin("THR_OperateCompanyApply THRO ON  THRO.VGsBm = tsell.Vgsbm");
			cond.addField("THRO.CUser AS cUser");
			cond = tpAmazonShopProductService.dataAuth(MENU_ID_WD, cond);
			List<Map<String, Object>> tpAmazonShopProductList = cond.queryMap();

			model.addAttribute("onProcess", 0);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID_WD,tpAmazonShopProductList);// 子列表配置信息

			return commonApiService.toEdit(model, tpProductActive.getId(), new TpProductActive(), MENU_ID_WD, null, subGridConfig);
			//return "business/tpProductActive/bindShop";
		}catch (Exception e) {
			logger.error("关联平台编码页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}


	}






}
